@extends('layouts.admin')
@section('content')
    <script src="{{URL::asset('js/ajaxfileupload.js')}}"></script>
    <div id="ribbon"><ol class="breadcrumb"><li>内容区</li></ol></div>
    <div class="content_wrapper">
        <aside>
            <header><i class="fa fa-fw fa-file"></i>添加首页Banner</header>
            <section>
                <div class="row">
                    <div class="col-sm-12">
                        <form id="dataForm" onsubmit="return false;" class="form-horizontal" role="form">
                            <div class="form-group">
                                <label class="col-sm-2 control-label"><span class="red">*</span>图片</label>
                                <div class="col-sm-3">
                                    <input type="hidden" name="pic" value="">
                                    <input type="file" id="pic" name="pic_file" onchange="uploadImage('pic')">
                                </div>
                            </div>
                            <hr/>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">外链</label>
                                <div class="col-sm-3">
                                    <input type="text" name="url" value="" class="form-control">
                                </div>
                            </div>
                            <hr/>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">序号</label>
                                <div class="col-sm-3">
                                    <input type="number" name="order_num" value="0" class="form-control" maxlength="20">
                                </div>
                            </div>
                            <hr/>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">状态</label>
                                <div class="col-sm-3">
                                    <select name="status" class="form-control">
                                        @foreach(\App\Model\Banner::$statusLabels as $k => $v)
                                            <option value="{{$k}}">{{$v}}</option>
                                        @endforeach
                                    </select>
                                </div>
                            </div>
                            <hr/>
                            <div class="form-group">
                                <label class="col-sm-2 control-label"> </label>
                                <div class=" col-sm-10">
                                    <input type="button" class="btn btn-primary" value="保存" id="saveBtn" onclick="save()" />&nbsp;&nbsp;&nbsp;
                                    <input type="button" class="btn btn-default" value="返回" onclick="history.go(-1)" />
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </section>
        </aside>
    </div>
    <script>
        function save() {
            $.post('{{url('admin/homeBanner/add')}}', $('#dataForm').serialize(), function (res) {
                if (res.status === 1) {
                    layer.msg(res.info, {icon: res.status}, function () {
                        location.href=document.referrer;
                    });
                } else {
                    layer.msg(res.info, {icon: res.status});
                }
            });
        }

        $('select[name="status"]').val(1);

        // 上传图片
        function uploadImage(elementId) {
            $.ajaxFileUpload({
                fileElementId: elementId,
                url: '{{url('api/upload/image')}}',
                type: 'post',
                dataType: 'json',
                secureuri: false, // 是否启用安全提交，默认为false。
                async : true,
                success: function(res) {
                    if (res.status === 1) {
                        $('#'+elementId).siblings('input[type="hidden"]').val(res.info);
                    } else {
                        layer.msg(res.info, {icon: 2});
                    }
                },
                error: function(data, status, e) {
                    layer.msg(e, {icon: 2});
                }
            });
        }
    </script>
@endsection